package com.chzu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chzu.pojo.*;
import com.chzu.pojo.QuestionPaper;
import com.chzu.pojo.vo.CreateExamPaperVo;
import com.chzu.result.JsonResult;
import com.chzu.service.PaperInfoService;
import com.chzu.service.QuestionPaperService;
import com.chzu.service.StudentInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Author lz
 * @Version 1.0
 * @Company 滁州大健康与养老产业研究院
 * @ClassName QuestionPaperController
 * @Description TODO
 * @Date 2022/02/22 17:41
 */
@RestController
@RequestMapping("/questionPaper")
@Api(tags = "题目试卷的接口")
public class QuestionPaperController {

    @Resource
    private QuestionPaperService questionPaperService;
    @Resource
    private PaperInfoService paperInfoService;
    @Resource
    private StudentInfoService studentInfoService;

    @ApiOperation("查询所有题目试卷信息")
    @PostMapping("/questionInfoPage")
    public JsonResult<List<QuestionPaper>> getAllQuestionPaper() {
        List<QuestionPaper> data = questionPaperService.list();
        return JsonResult.ok(data);
    }

    @ApiOperation("添加某个题目试卷信息")
    @Transactional(rollbackFor= Exception.class)
    @PostMapping("/")
    public JsonResult<String> addQuestionPaper(@RequestBody QuestionPaper questionPaper) {
        boolean save = questionPaperService.save(questionPaper);
        if (!save) {
            return JsonResult.fail("添加试题目试卷信息失败");
        }
        return JsonResult.ok("添加题目试卷信息成功");
    }

    @ApiOperation("删除某个题目试卷信息")
    @Transactional(rollbackFor= Exception.class)
    @DeleteMapping("/deleteQuestionPaper")
    public JsonResult<String> deleteQuestionPaper(@RequestBody QuestionPaper questionPaper) {
        // 根据试卷编号和题目编号删除信息
        QueryWrapper<QuestionPaper> wrapper = new QueryWrapper<>();
        wrapper.eq("question_id",questionPaper.getQuestionId());
        wrapper.eq("paper_id",questionPaper.getPaperId());
        boolean result = questionPaperService.remove(wrapper);
        if (!result) {
            return JsonResult.fail("删除题目试卷信息失败");
        }
        return JsonResult.ok("删除题目试卷信息成功");
    }

    @ApiOperation("创建试卷试题")
    @Transactional(rollbackFor= Exception.class)
    @PostMapping("/createPaper")
    public JsonResult<Object> createPaper(@RequestBody CreateExamPaperVo createExamPaperVo) {
        /*---------------生成试卷标题，测试时间------------------------*/
        createExamPaperVo.setPaperId(UUID.randomUUID().toString());
        createExamPaperVo.setPaperDate(new Date());
        // 获得用户的账号
        SysUser user = (SysUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String userName = user.getUsername();
        // 根据账号查询用户姓名
        QueryWrapper<StudentInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name",userName);
        StudentInfo studentInfo = studentInfoService.getOne(wrapper);
        // 设置出题老师的姓名
        createExamPaperVo.setTeacherId(userName);
        PaperInfo paperInfo = new PaperInfo();
        paperInfo.setPaperId(createExamPaperVo.getPaperId());
        paperInfo.setPaperDate(createExamPaperVo.getPaperDate());
        paperInfo.setPaperTime(createExamPaperVo.getPaperTime());
        paperInfo.setPaperName(createExamPaperVo.getPaperName());
        paperInfo.setTeacherId(createExamPaperVo.getTeacherId());
        if (!paperInfo.getPaperDate().before(paperInfo.getPaperTime())) {
            return JsonResult.fail("考试时间应晚于当前时间");
        }
        boolean save = paperInfoService.save(paperInfo);
        if (!save) {
            return JsonResult.fail("添加试卷信息失败");
        }
        /*----------------创建出试卷的试题-----------------------*/
        List<String> questionId = createExamPaperVo.getQuestionId();
        System.out.println("------------------>" + questionId);
        String paperId = createExamPaperVo.getPaperId();
        System.out.println("------------------>" + paperId);
        int res = questionPaperService.createPaper(paperId,questionId);
        if (res <= 0 ) {
            return JsonResult.fail("创建试卷失败");
        }
        return JsonResult.ok("创建试卷成功");
    }
}
